﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ruandao.HostServer.Json
{
    using Ruandao.Misc;

    public class JsonDatabase : Database
    {
        internal Database SetFromEntity(DatabaseEntity entity)
        {
            TypeUtil.MapProperty(entity, this,
                "DatabaseId",
                "DbType",
                "Instance",
                "Connection",
                "AccessType");
            return this;
        }

        static public IDictionary<int, Database> GetAllDatabases()
        {
            ObjectCache cache = MemoryCache.Default;
            string cacheKey = "ALL_DATABASE_OBJECTS";
            IDictionary<int, Database> databases = cache[cacheKey] as IDictionary<int, Database>;
            if (databases == null)
            {
                databases = new Dictionary<int, Database>();
                foreach (var e in DatabaseEntity.GetAllEntities())
                {
                    databases.Add(e.DatabaseId, new Database().SetFromEntity(e));
                }
                CacheItemPolicy policy = new CacheItemPolicy();
                cache.Set(cacheKey, databases, policy);
            }
            return databases;
        }

    }
}
